package cc.cary.warehouse.controller;

import cc.cary.warehouse.client.RequestClient;
import cc.cary.warehouse.client.dto.DataDTO;
import cc.cary.warehouse.client.dto.RespDTO;
import cc.cary.warehouse.client.form.PostRequestForm;
import cc.cary.warehouse.common.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author Cary
 * @date 2023/4/23
 */
@Tag(name = "客户端请求管理", description = "客户端请求相关接口啦")
@RestController
@RequestMapping("/v1/client")
public class ClientController {
    @Autowired
    private RequestClient requestClient;

    @Operation(summary = "测试GET请求")
    @GetMapping("/testGet")
    public R<DataDTO> testGet(@RequestParam String name) {
        RespDTO<DataDTO> resp = requestClient.testGet(name);
        return R.ok(resp.getData());
    }

    @Operation(summary = "测试POST请求")
    @PostMapping("/testPost")
    public R<String> testPost(@Valid @RequestBody PostRequestForm form) {
        RespDTO<String> resp = requestClient.testPost(form);
        return R.ok(resp.getMsg());
    }

    @Operation(summary = "测试异步GET请求")
    @GetMapping("/testAsyncGet")
    public R<DataDTO> testAsyncGet(@RequestParam String name) throws ExecutionException, InterruptedException {
        Future<RespDTO<DataDTO>> future = requestClient.testAsyncGet(name);
        RespDTO<DataDTO> respDTO = future.get();
        return R.ok(respDTO.getData());
    }
}
